package cn.com.server.config;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.web.servlet.FilterRegistrationBean;
import org.springframework.boot.web.servlet.ServletRegistrationBean;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import com.alibaba.druid.support.http.StatViewServlet;
import com.alibaba.druid.support.http.WebStatFilter;
import cn.com.server.config.bean.AppGlobal;

@Configuration
public class GlobalConfig {
	
	@Autowired
	private AppGlobal appGlobal;
	

//    /**
//     * mybatis-plus SQL执行效率插件【生产环境可以关闭】
//     */
//	@Bean
//    public PerformanceInterceptor performanceInterceptor() {
//        return new PerformanceInterceptor();
//    }
//	
//	/**
//	 * mybatis-plus分页插件
//	 * 
//	 */
//	@Bean
//    public PaginationInterceptor paginationInterceptor() {
//        PaginationInterceptor page = new PaginationInterceptor();
//        if(appGlobal.getDbType()!=null&&!"".equals(appGlobal.getDbType())) {
//        	page.setDialectType(appGlobal.getDbType());
//        }else {
//        	page.setDialectType("mysql");
//        }
//        return page;
//    }
	
	/**
     * 注册一个StatViewServlet
     * @return
     */
    @Bean
    public ServletRegistrationBean DruidStatViewServle(){
        //org.springframework.boot.context.embedded.ServletRegistrationBean提供类的进行注册.
    	ServletRegistrationBean servletRegistrationBean = null;
    	if(appGlobal.getDruidPath()!=null&&!"".equals(appGlobal.getDruidPath())) {
    		servletRegistrationBean = new ServletRegistrationBean(new StatViewServlet(),appGlobal.getDruidPath());
        }else {
        	servletRegistrationBean = new ServletRegistrationBean(new StatViewServlet(),"/druid/*");
        }
    	//添加初始化参数：initParams
    	//白名单：
    	if(appGlobal.getDruidAllowIp()!=null&&!"".equals(appGlobal.getDruidAllowIp())) {
    		 servletRegistrationBean.addInitParameter("allow",appGlobal.getDruidAllowIp());
    	}
    	//IP黑名单 (存在共同时，deny优先于allow) : 如果满足deny的话提示:Sorry, you are not permitted to view this page.
    	if(appGlobal.getDruidDenyIp()!=null&&!"".equals(appGlobal.getDruidDenyIp())) {
    		 servletRegistrationBean.addInitParameter("deny",appGlobal.getDruidAllowIp());
    	}
        //登录查看信息的账号密码.
    	if(appGlobal.getDruidLoginUserName()!=null&&!"".equals(appGlobal.getDruidLoginUserName())) {
    		servletRegistrationBean.addInitParameter("loginUsername",appGlobal.getDruidLoginUserName());
    	}else {
    		servletRegistrationBean.addInitParameter("loginUsername","admin");
    	}
        if(appGlobal.getDruidLoginPassword()!=null&&!"".equals(appGlobal.getDruidLoginPassword())) {
        	servletRegistrationBean.addInitParameter("loginPassword",appGlobal.getDruidLoginPassword());
    	}else {
    		servletRegistrationBean.addInitParameter("loginPassword","admin");
    	}
        //是否能够重置数据.
        if(appGlobal.getDruidResetEnable()!=null&&!"".equals(appGlobal.getDruidResetEnable())) {
        	servletRegistrationBean.addInitParameter("resetEnable",appGlobal.getDruidResetEnable());
        }else {
        	servletRegistrationBean.addInitParameter("resetEnable","false");
        }
        return servletRegistrationBean;
    }
    
    /**
     * 注册一个：filterRegistrationBean
     * @return
     */
    @Bean
    public FilterRegistrationBean druidStatFilter(){
        FilterRegistrationBean filterRegistrationBean = new FilterRegistrationBean(new WebStatFilter());
        //添加过滤规则.
        if(appGlobal.getFilterUrlPatterns()!=null&&!"".equals(appGlobal.getFilterUrlPatterns())) {
        	filterRegistrationBean.addUrlPatterns(appGlobal.getFilterUrlPatterns());
        }else {
        	filterRegistrationBean.addUrlPatterns("/*");
        }
        //添加不需要忽略的格式信息.
        if(appGlobal.getFilterExclusions()!=null&&!"".equals(appGlobal.getFilterExclusions())) {
        	filterRegistrationBean.addInitParameter("exclusions",appGlobal.getFilterExclusions());
        }else {
        	filterRegistrationBean.addInitParameter("exclusions","*.js,*.gif,*.jpg,*.png,*.css,*.ico,/druid/*");
        }
        return filterRegistrationBean;
    }
}
